package com.itlhc.service.Impl;

import com.itlhc.dao.UserMapper;
import com.itlhc.pojo.User;
import com.itlhc.service.UserService;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UserServiceImpl implements UserService {

    private UserMapper userMapper;

    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public User loinUser(String username, String password) {
        return userMapper.loinUser(username,password);
    }

    @Override
    public void addCookie(String username, String password, HttpServletResponse response, HttpServletRequest request) {
        if(!username.equals("") && !password.equals("")){
            //创建  Cookie
            Cookie loginAccountCookie = new Cookie("username",username);
            Cookie loginPasswordCookie = new Cookie("password",password);
            //设置Cookie的父路经
            loginAccountCookie.setPath(request.getContextPath()+"/");
            loginPasswordCookie.setPath(request.getContextPath()+"/");
            //获取是否保存Cookie（例如：复选框的值）
            String rememberMe = request.getParameter("rememberMe");
            //if( rememberMe==null || rememberMe.equals(false) ){
            if( rememberMe==null){
                //不保存Cookie
                loginAccountCookie.setMaxAge(0);
                loginPasswordCookie.setMaxAge(0);
            }else{
                //保存Cookie的时间长度，单位为秒
                loginAccountCookie.setMaxAge(7*24*60*60);
                loginPasswordCookie.setMaxAge(7*24*60*60);
            }
            //加入Cookie到响应头
            response.addCookie(loginAccountCookie);
            response.addCookie(loginPasswordCookie);
        }
    }
}
